package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.events.Event;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass= RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    //本地注入
    @Autowired
    private RoleDao roleDao;

    //分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //获取一页几行
        Integer pageSize = queryPageBean.getPageSize();
        //获取当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取查找的字符串
        String queryString = queryPageBean.getQueryString();
        //利用分页助手
        PageHelper.startPage(currentPage,pageSize);
        //调用dao层方法
        Page<Role> page = roleDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Role> rows = page.getResult();
        return new PageResult(total,rows);
    }

    //按角色Id查询
    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    //根据角色Id查询权限Id
    @Override
    public List<Integer> findPermissionIdByRoleId(Integer id) {
        return roleDao.findPermissionIdByRoleId(id);
    }
   //根据权限Id查找菜单Id
    @Override
    public List<Integer> findMenuIdByRoleId(Integer id) {
        return roleDao.findMenuIdByRoleId(id);
    }

    //新建
    @Override
    public void add(Role role, Integer[] permissions, Integer[] menus) {
        //建立角色表
        roleDao.add(role);
        //获取Id
        Integer roleId = role.getId();
        //建立关系
        this.setRoleAndPermission(roleId,permissions);

        this.setRoleAndMenu(roleId,menus);

    }
  //删除
    @Override
    public void delete(Integer id) {
        //解除角色与权限的关系
        roleDao.upsetRoleAndPermission(id);
        //解除角色与菜单的关系
        roleDao.upsetRoleAndMenu(id);
        //先删除月色表中角色
        roleDao.delete(id);
    }

    //编辑角色
    @Override
    public void edit(Role role, Integer[] permissions, Integer[] menus) {
        //修改角色
        roleDao.edit(role);
        //获取角色Id
        Integer roleId = role.getId();
        //取消建立的关系
        roleDao.upsetRoleAndMenu(roleId);
        roleDao.upsetRoleAndPermission(roleId);

        //重新建立联系
        //菜单
        this.setRoleAndMenu(roleId,menus);
        //权利
        this.setRoleAndPermission(roleId,permissions);
    }


    //建立角色与与菜单关系
    public void setRoleAndMenu(Integer roleId,Integer[] menus){
        //获取菜单ID 需要遍历
        if(menus != null && menus.length > 0){
            for (Integer menu : menus) {
                Map<String,Integer> map=new HashMap<>();
                map.put("roleId",roleId);
                map.put("menuId",menu);
                roleDao.setRoleAndMenu(map);
            }
        }
    }


    //建立角色与与权限关系
    public void setRoleAndPermission(Integer roleId,Integer[] permissions){
        //获取权限ID 需要遍历
        if(permissions != null && permissions.length > 0){
            for (Integer permission : permissions) {
                Map<String,Integer> map=new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permission);
                roleDao.setRoleAndPermission(map);
            }
        }
    }


   // 取消 角色 与 权限关联的Id



}
